package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.qo.EmployeeQueryObject;
import cn.wolfcode.wms.service.IDepartmentService;
import cn.wolfcode.wms.service.IEmployeeService;
import cn.wolfcode.wms.service.IRoleService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.PageResult;
import cn.wolfcode.wms.util.RequiredPermission;
import org.omg.CORBA.OBJECT_NOT_EXIST;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Optional;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    @RequestMapping("list")
    public String list(@ModelAttribute("qo")EmployeeQueryObject qo, Model model){
        PageResult result = employeeService.query(qo);
        model.addAttribute("result",result);
        model.addAttribute("depts",departmentService.list());
        return "employee/list";
    }
    @RequestMapping("input")
    public String input(Long id,Model model){
        //查询所有部门信息,存入模型中
        model.addAttribute("depts",departmentService.list());
        //查询所有角色信息,存入模型中
        model.addAttribute("roles",roleService.list());
        if (Optional.ofNullable(id).isPresent()) {
            model.addAttribute("entity",employeeService.get(id));
        }
        return "employee/input";
    }

    @RequiredPermission("删除员工")
    @RequestMapping("delete")
    public String delete(Long id){
        if (Optional.ofNullable(id).isPresent()) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list.do";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Employee entity ,Long[] roleIds){
        employeeService.saveOrUpdate(entity,roleIds);
        return new JSONResult();
    }

    @RequestMapping("batchDelete")
    @ResponseBody
    public Object batchDelete(Long[] ids){
        employeeService.batchDelete(ids);
        return new JSONResult();
    }
}
